import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  ElementRef,
  OnInit,
  AfterViewInit,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PlanCategory,
  ComboboxItemDto,
  GetTabDtosForTabOutput,
  MonthPlanServiceProxy,
} from "@shared/service-proxies/service-proxies";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { Table } from "primeng/components/table/table";
import { Paginator } from "primeng/components/paginator/paginator";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import * as moment from "moment";
import { finalize } from "rxjs/operators";

@Component({
  templateUrl: "./materialready.component.html",
  styleUrls: ["./materialready.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class MaterialreadyComponent
  extends AppComponentBase
  implements AfterViewInit {
  @ViewChild("dataTable") dataTable: Table;
  @ViewChild("paginator") paginator: Paginator;

  @ViewChild("dateTimePicker") dateTimePicker: ElementRef;
  advancedFiltersAreShown = false;
  anyncing = false;
  filterText = "";
  planDate = new Date();
  constructor(
    injector: Injector,
    private _monthServiceProxy: MonthPlanServiceProxy
  ) {
    super(injector);
  }

  ngAfterViewInit() {}
  getMonths(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._monthServiceProxy
      .getReadyMaterials(
        this.filterText,
        this.planDate.getFullYear(),
        this.planDate.getMonth() + 1,
        this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe((result) => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }
}
